package de.justphil.tcg.tcgclient;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.JLabel;

public class TCGClient extends JFrame {

	private static final long serialVersionUID = -3505422005253754820L;
	private final JPanel contentPane;
	private final JTextField uriTextField;
	private final JTextArea inputTextArea;
	private final JScrollPane inputScrollPane;
	private final JTextArea outputTextArea;
	private final JScrollPane outputScrollPane;
	private final JLabel connectedLabel;
	private final JButton connectButton;
	private final JButton sendButton;
	
	private MyWebSocketClient ws;
	
	public static final String NOT_CONNECTED	= "Not connected!";
	public static final String CONNECTED		= "Connected!";
	public static final String CONNECT			= "Connect";
	public static final String DISCONNECT		= "Disconnect";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		/* Set Native Look and Feel for the corresponding OS */
		try {
			UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
			System.out.println("System LAF set!");
		}
		catch( Exception e ) {
			System.out.println("System LAF couldn't be set!");
			e.printStackTrace();
		}
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TCGClient frame = new TCGClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private void connectButtonClicked() {
		if (connectButton.getText().equals(CONNECT)) {
			System.out.println("Connecting to " + uriTextField.getText());
			ws = new MyWebSocketClient(uriTextField.getText(), this);
			ws.connect();
		}
		else {
			ws.close();
			ws = null;
		}
	}

	/**
	 * Create the frame.
	 */
	public TCGClient() {
		setResizable(false);
		setTitle("[Trading Card Game] WebSocket Client v0.1");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1000, 597);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		uriTextField = new JTextField();
		uriTextField.setBounds(10, 11, 875, 20);
		uriTextField.setText("ws://localhost:8888/game");
		contentPane.add(uriTextField);
		uriTextField.setColumns(10);
		
		connectButton = new JButton(CONNECT);
		connectButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				TCGClient.this.connectButtonClicked();
			}
		});
		connectButton.setBounds(895, 10, 89, 23);
		contentPane.add(connectButton);
		
		inputTextArea = new JTextArea();
		inputTextArea.setBounds(10, 42, 974, 240);
		
		inputScrollPane = new JScrollPane(inputTextArea);
		inputScrollPane.setBounds(10, 42, 974, 240);
		contentPane.add(inputScrollPane);
		
		outputTextArea = new JTextArea();
		outputTextArea.setBounds(10, 293, 974, 240);
		
		outputScrollPane = new JScrollPane(outputTextArea);
		outputScrollPane.setBounds(10, 293, 974, 240);
		outputScrollPane.getVerticalScrollBar().setVisible(true);
		contentPane.add(outputScrollPane);
		
		JLabel statusLbl = new JLabel("Status:");
		statusLbl.setBounds(839, 544, 46, 14);
		contentPane.add(statusLbl);
		
		connectedLabel = new JLabel(NOT_CONNECTED);
		connectedLabel.setBounds(902, 544, 82, 14);
		contentPane.add(connectedLabel);
		
		sendButton = new JButton("Send Message");
		sendButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (connectButton.getText().equals(DISCONNECT) && ws.isConnected()) {
					ws.sendMessage(inputTextArea.getText());
					//inputTextArea.setText("");
				}
			}
		});
		sendButton.setBounds(10, 540, 819, 23);
		contentPane.add(sendButton);
	}

	public JPanel getContentPane() {
		return contentPane;
	}

	public JTextField getUriTextField() {
		return uriTextField;
	}

	public JTextArea getInputTextArea() {
		return inputTextArea;
	}

	public JTextArea getOutputTextArea() {
		return outputTextArea;
	}
	
	public JScrollPane getOutputScrollPane() {
		return outputScrollPane;
	}

	public JLabel getConnectedLabel() {
		return connectedLabel;
	}

	public JButton getConnectButton() {
		return connectButton;
	}

	public JButton getSendButton() {
		return sendButton;
	}
	
}
